/* =====================
 * SVGBarChartDemo1.java
 * =====================
 * 
 * Copyright (c) 2013-2017, Object Refinery Limited.
 * All rights reserved.
 *
 * http://www.jfree.org/jfreechart/index.html
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of the Object Refinery Limited nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL OBJECT REFINERY LIMITED BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

package org.jfree.graphics2d.svg.demo;

import java.awt.*;
import java.io.File;
import java.io.IOException;

import com.elizabeth.CategoryImageAnnotation;
import com.elizabeth.ExChartUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.CategoryTextAnnotation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.ui.TextAnchor;
import org.jfree.chart.ui.UIUtils;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;

import javax.swing.*;

/**
 * A demo/test for a bar chart.
 */
public class SVGBarChartDemo1 extends ApplicationFrame {

    public SVGBarChartDemo1(String title) {
        super(title);
        ChartPanel chartPanel = (ChartPanel)createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(1000, 570));
        setContentPane(chartPanel);
    }

    public static JPanel createDemoPanel(){
        ChartPanel panel = new ChartPanel(createChart(createDataset()), false);
        panel.setMouseWheelEnabled(true);
        panel.setFillZoomRectangle(true);
        return panel;
    }

    /**
     * Creates a sample dataset.
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {
        DefaultCategoryDataset dataset
            = new DefaultCategoryDataset();
        dataset.addValue(22.0, "Row 1", "Column 1");
        dataset.addValue(18.0, "Row 1", "Column 2");
        dataset.addValue(28.0, "Row 1", "Column 3");
        dataset.addValue(17.0, "Row 1", "Column 4");
        dataset.addValue(10.0, "Row 2", "Column 1");
        dataset.addValue(15.0, "Row 2", "Column 2");
        dataset.addValue(13.0, "Row 2", "Column 3");
        dataset.addValue(7.0, "Row 2", "Column 4");
        return dataset;
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  a dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(CategoryDataset dataset) {

        JFreeChart chart = ChartFactory.createLineChart(
            "Statistical Bar Chart Demo 1", "Type", "Value", dataset);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();

        plot.addAnnotation(new CategoryTextAnnotation("Hello", "Column 1",25f));
        plot.addAnnotation(new CategoryImageAnnotation("",
                ExChartUtils.getExScaledInstance(new ImageIcon("src/main/resources/icon-png/e465a48291ed2a8da539e4ca7aebc202.jpg"),40,40),
                "Column 2",21f,20d));

        plot.addAnnotation(new CategoryImageAnnotation("",
                ExChartUtils.getExScaledInstance(new ImageIcon("src/main/resources/icon-png/中雨.png"),40,40),
                        "Column 2",23.5f,20d));


        // customise the range axis...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(false);


        CategoryAxis domainAxis = plot.getDomainAxis();
        //domainAxis.setLowerMargin(0.05);
		//domainAxis.setUpperMargin(0.05);
        domainAxis.setCategoryMargin(0.6);


        // customise the renderer...
        BarRenderer renderer = new BarRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setIncludeBaseInRange(false);
        plot.setRenderer(renderer);

        // ensure the current theme is applied to the renderer just added
        //ChartUtils.applyCurrentTheme(chart);

        renderer.setDefaultItemLabelGenerator(
                new StandardCategoryItemLabelGenerator());
        renderer.setDefaultItemLabelsVisible(true);
        renderer.setDefaultItemLabelPaint(Color.black);
        renderer.setDefaultPositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.INSIDE12, TextAnchor.TOP_CENTER));

        System.out.println(renderer.getMaximumBarWidth());
        renderer.setItemMargin(0f);
        // set up gradient paints for series...
        GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, new Color(240, 201, 86),
                0.0f, 0.0f, new Color(240, 201, 86));
        GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, new Color(60, 115, 214),
                0.0f, 0.0f, new Color(60, 115, 214));
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);

        return chart;
    }

    /**
     * Starting point for the demo.
     * 
     * @param args  ignored.
     * 
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
       SVGBarChartDemo1 demo = new SVGBarChartDemo1("svg-demo");
        demo.pack();
        UIUtils.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}
